package com.qishenyuan.competition.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qishenyuan.competition.common.PageUtil;
import com.qishenyuan.competition.dto.JobRecommendationReq;
import com.qishenyuan.competition.model.JobRecommendation;
import com.qishenyuan.competition.dao.JobRecommendationDao;
import com.qishenyuan.competition.service.JobRecommendationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qishenyuan.competition.vo.JobRecommendationBriefResp;
import com.qishenyuan.competition.vo.JobRecommendationResp;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 内推职位信息表 服务实现类
 * </p>
 *
 * @author qishenyuan
 * @since 2025-03-25
 */
@Service
public class JobRecommendationServiceImpl extends ServiceImpl<JobRecommendationDao, JobRecommendation> implements JobRecommendationService {

    @Override
    public IPage<JobRecommendationBriefResp> getJobRecommendationList(JobRecommendationReq jobRecommendationReq) {
        IPage<JobRecommendation> iPage = PageUtil.toIPage(jobRecommendationReq.getPageQuery());
        QueryWrapper<JobRecommendation> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        IPage<JobRecommendation> iPageResult = baseMapper.selectPage(iPage, queryWrapper);
        return iPageResult.convert(jobRecommendation -> JobRecommendationBriefResp.buildVo(jobRecommendation));
    }

    @Override
    public JobRecommendationResp getJobRecommendation(Integer id) {
        JobRecommendation jobRecommendation = baseMapper.selectById(id);
        if(jobRecommendation == null){
            return null;
        }
        return JobRecommendationResp.buildVo(jobRecommendation);
    }

    @Override
    public List<JobRecommendationBriefResp> searchJobRecommendation(String key) {
        QueryWrapper<JobRecommendation> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(key),"title",key);
        List<JobRecommendation> jobRecommendations = baseMapper.selectList(queryWrapper);

        return jobRecommendations
                .stream()
                .map(JobRecommendationBriefResp::buildVo)
                .collect(Collectors.toList());
    }
}
